package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 24270
 */
@Controller
public class UploadController {

    @RequestMapping("/upload")
    public void upload(@RequestParam("picture") MultipartFile picture) throws Exception {
        System.out.println(picture.getOriginalFilename());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String newFileName = sdf.format(new Date()) + "-" + picture.getOriginalFilename();
        // 保存在D://upload中
        File targetFile = new File("/upload/" + newFileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //保存文件
        picture.transferTo(targetFile);
        System.out.println(targetFile.getAbsolutePath());
    }

    @RequestMapping("/uploadView")
    public ModelAndView upload() {
        return new ModelAndView("upload");
    }

}
